package com.cml.dm.admin.service.sql;

import com.cml.dm.admin.entity.DmExeLogger;
import com.cml.dm.admin.entity.DmMetadata;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class JoinNodeDeleteRecoverySqlGenerateService  extends  DefaultDeleteRecoverySqlGenerateService{
    @Override
    public List<String> sqlGenerate(DmMetadata dmMetadata, DmExeLogger dmExeLogger, Object extParameter) {

        DmMetadata upMetadata =dmMetadata.getUpDmMetadata();
        List<String> sqlList = new ArrayList<>();
        String sf = "delete from %s where %s in (%s)";
        DmMetadata joinNode = dmMetadata.getJoinNode();
        if(null == joinNode){
            joinNode = upMetadata;
        }
        //根据join字段替换生成sql语句
        String upNodeSql =  "select " + joinNode.getPk()  +" from " +joinNode.getTable() +" where "+ joinNode.getOrgId() +"=" + dmExeLogger.getOrgId();

        String sql = String.format(sf,dmMetadata.getTable(),dmMetadata.getJoinUpPkNode(),upNodeSql);
        sqlList.add(sql);
        return sqlList;
    }
}
